<template>
  <el-card class="gray-card no-border no-margin-top" shadow="never">
    <el-form ref="form" :model="form" class="form">
      <span>基本信息</span>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计划编号：">
            <el-input v-model="form.planNo" :disabled="disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划名称：">
            <el-input v-model="form.planName" :disabled="disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="年度计划：">
            <el-select v-model="form.planYear"
                       :disabled="disabled"
                       placeholder="请选择年度计划">
              <el-option
                v-for="item in planList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="质评轮次：">
            <el-select v-model="form.planRound"
                       :disabled="disabled"
                       placeholder="请选择质评轮次">
              <el-option
                v-for="item in roundList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="仪器编号：">
            <el-select v-model="form.instrumentNo"
                       :disabled="disabled"
                       @change="handleInstrumentChange"
                       placeholder="请选择仪器">
              <el-option
                v-for="item in insturmentList"
                :key="item.instrumentNo"
                :label="item.instrumentName"
                :value="item.instrumentNo">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仪器名称：">
            <el-input v-model="form.instrumentName" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="试剂提供商：">
            <el-input v-model="form.reagent" :disabled="disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校准物提供商：">
            <el-input v-model="form.standardSubstance" :disabled="disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="检验方法：">
            <el-input v-model="form.checkMethod" :disabled="disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属组：">
            <el-input v-model="form.organizeName" :disabled="disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <span>质评结果</span>
      <el-row>
        <el-col :span="12">
          <el-form-item label="质评结果：">
            <el-select v-model="form.qualityResult"
                       :disabled="disabled"
                       placeholder="请选择质评结果">
              <el-option
                v-for="item in statusList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="本组实验室数：">
            <el-input-number
              v-model="form.experimentNum"
              :min="1"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="质评成绩：">
            <el-input-number
              v-model="form.qualityScore"
              :min="0"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="统计日期：">
            <el-date-picker
              v-model="form.checkTime"
              type="datetime"
              placeholder="选择统计日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="室间质评报告：">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

    </el-form>
    <div class="details-foot">
      <div class="cell-btn">
        <el-button type="primary" @click.native="onSubmit" v-if="!disabled">提交</el-button>
        <el-button @click.native="handleClose">取消</el-button>
      </div>
    </div>
  </el-card>
</template>

<script>
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {parseTime} from '@/utils/ruoyi'
import {queryList} from "@/api/business/instrument"
import {addSpaceQuality, updateSpaceQuality} from "@/api/business/spaceQuality"

export default {
  name: "addexamine",
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      planList: [
        {
          label: '2021年',
          value: '2021'
        },
        {
          label: '2020年',
          value: '2020'
        },
        {
          label: '2019年',
          value: '2019'
        }
      ],
      roundList: [
        {
          label: '第一轮次',
          value: '第一轮次'
        },
        {
          label: '第二轮次',
          value: '第二轮次'
        },
        {
          label: '第三轮次',
          value: '第三轮次'
        }
      ],
      form: {
        id: null,
        planNo: '',
        planName: '',
        planYear: '',
        planRound: '',
        instrumentNo: '',
        instrumentName: '',
        reagent: '',
        standardSubstance: '',
        checkMethod: '',
        organizeName: '',
        qualityResult: '',
        experimentNum: '',
        qualityScore: '',
        checkTime: '',
        fileList: [],
        dbStatus: 1
      },
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      statusList: [
        {
          label: '通过',
          value: '0'
        },
        {
          label: '不通过',
          value: '1'
        }
      ],
      insturmentList: [],
    };
  },
  components: {Treeselect},
  watch: {},
  created() {
    this.$nextTick(() => {
      this.getTreeselect();
      this.handleGetInsturmentList();
    })
  },
  methods: {
    onSubmit() {
      this.form.checkTime = parseTime(this.form.checkTime)
      if (this.form.id) {
        updateSpaceQuality(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      } else {
        addSpaceQuality(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      }
    },
    handleData(obj) {
      this.form = obj;
    },
    handleInstrumentChange() {
      this.form.instrumentName = this.insturmentList.filter(x => x.instrumentNo == this.form.instrumentNo)[0].instrumentName
    },
    handleNodeClick(data) {
      this.form.deptName = data.label
    },
    handleChangeProject() {
      this.form.projectName = this.prjectList.filter(x => x.id == this.form.projectId)[0].itemName
    },
    handleClose() {
      this.form = {}
      this.$emit('close', false)
    },
    // 获取仪器数据
    handleGetInsturmentList() {
      queryList().then((res) => {
        this.insturmentList = res
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    }
  }
};
</script>
